<?php
/**
 * Application level Controller
 *
 * This file is application-wide controller file. You can put all
 * application-wide controller-related methods here.
 *
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
 * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
 *
 * Licensed under The MIT License
 * For full copyright and license information, please see the LICENSE.txt
 * Redistributions of files must retain the above copyright notice.
 *
 * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
 * @link          http://cakephp.org CakePHP(tm) Project
 * @package       app.Controller
 * @since         CakePHP(tm) v 0.2.9
 * @license       http://www.opensource.org/licenses/mit-license.php MIT License
 */
App::uses('Controller', 'Controller');

/**
 * Application Controller
 *
 * Add your application-wide methods in the class below, your controllers
 * will inherit them.
 *
 * @package		app.Controller
 * @link		http://book.cakephp.org/2.0/en/controllers.html#the-app-controller
 */
class EgitimsController extends AppController {
	public $helpers = array('Html', 'Form');
	public $components = array('Paginator');
	public $paginate = array(
		'limit' => 5,
		'order' => array(
			'Egitim.title' => 'asc'
		)
	);
	public function index() {
		$this->set('egitims', $this->Egitim->find('all'));
	}
	
	public function listEgitim () {
		$this->layout = null ;
		
		$projeId = $this->request->query('projeId');

		$conditions = array();
		$conditions['Egitim.baslik LIKE'] = $this->request->query('baslik') . '%';
		if ($this->isKatilimci()) {
			$grupProgramEvent = ClassRegistry::init('ProjesGrupProgramEvents');
			$katilimci = $this->getKatilimci();
			debug($katilimci, true);
			$egitimIds = $grupProgramEvent->find('list', array(
				'fields' => array('egitim_id'),
				'conditions'=> array('ProjesGrupProgramEvents.proje_id = ' => $katilimci['proje_id']),
				'group' => 'egitim_id'
			));
			if ($egitimIds) {
				//array_push($conditions, "Egitim.id in ".$egitimIds);
				$conditions['Egitim.id '] = $egitimIds;
			}
		}
		
		$this->Paginator->settings = $this->paginate;
		$this->Paginator->settings = array(
			'conditions' => $conditions,
			'limit' => $this->request->query('limit'),
			'page' => $this->request->query('page')
		);
		
		
		
		if ($this->request->query('sort')) {
			$sortArr = json_decode($this->request->query('sort'), true);
			if (count($sortArr) == 1) {
				$property =  $sortArr[0]['property'];
				$direction =  $sortArr[0]['direction'];
				$this->Paginator->settings['order'] =  array(
					'Egitim.' . $property => $direction
				);
			}			
		}

		// similar to findAll(), but fetches paged results
		$data = $this->Paginator->paginate('Egitim');
		//$this->Paginator->counter();//$this->Paginator->paging['count']; //$this->Paginator->find('count');
		//echo $count . "xxxx";
		$count = $this->params['paging']['Egitim']['count'];
		$this->set('egitims', $data);
		$this->set('count', $count);
	}
	
	public function save() {
		$this->layout = null ;
		$this->autoRender = false;
		if ($this->request->is('post')) {
			$this->Egitim->create();
			$result = $this->Egitim->save($this->request->data);
			if ($result) {
				echo "{msg:'Eğitim Başarıyla Kaydedildi', egitimId:" . $this->Egitim->id .  ", success: true}";
			} else {
				echo "{msg:'hatalı', success: false}";
			}
		}
	}
	
	/**
	 * Eğitmenin girdiği eğitim içeriğini kaydededer
	 * egitim_id, egitmen_id, alt_baslik ve aciklama alanlarının gelmesi gerekir
	 */
	public function saveEgitimIcerik() {
		$this->autoRender = false;
		$this->layout = 'ajax';
		
		$egitimIcerik = ClassRegistry::init('EgitimsIcerik');
		
		
		if ($this->request->is('post')) {
			$result = FALSE;
			$data = $this->request->data;
			$ei = $egitimIcerik->find('first', array(
				'conditions' => array(
					'EgitimsIcerik.egitim_id' => $this->request->data('egitim_id'),
					'EgitimsIcerik.egitmen_id' => $this->request->data('egitmen_id')
				)
			));
			if ($ei) {
				$data['id'] = $ei['EgitimsIcerik'] ['id'];
				$result = $egitimIcerik->save($data);
			} else {
				$egitimIcerik->create();
				$result = $egitimIcerik->save($this->request->data);
			}
			
			if ($result) {
				echo "{msg:'Eğitim İçeriği Başarıyla Kaydedildi', success: true}";
			} else {
				echo "{msg:'hatalı', success: false}";
			}
		}
	}
	
	/**
	 * 
	 * @param string $egitimId
	 * @param string $egitmenId
	 */
	public function loadEgitimIcerik($egitmenId = null, $egitimId = null) {
		
		$this->autoRender = false;
		$this->layout = null;
		
		$egitimIcerik = ClassRegistry::init('EgitimsIcerik');
		debug($egitimId, true);
		$ei = $egitimIcerik->find('first', array(
				'conditions' => array(
						'EgitimsIcerik.egitim_id' => $egitimId,
						'EgitimsIcerik.egitmen_id' => $egitmenId
				)
		));
		debug($ei, true);
		if ($ei) {
			echo '{data: ' .json_encode($ei['EgitimsIcerik']) . ', success: true}';
		} else {
			$ei = $egitimIcerik->create()['EgitimsIcerik'];
			$ei->egitmen_id = $egitmenId;
			$ei->egitim_id = $egitimId;
			$ei->aciklama = null;
			$ei->alt_baslik = null;
			echo '{data: '. json_encode($ei) . ', success: true}';
		}
	}
	
	public function delete($id = null) {
		//$this->layout = null ;
		$this->autoRender = false;
		$this->layout = 'ajax';
		
		$this->Egitim->id = $id;
		
		if ($this->Egitim->delete()) {
			echo "{msg:'Eğitim Başarıyla Silindi', success: true}";
		} else {
			echo "{msg:'hatalı', success: false}";
		}
	
	}
	
	public function load($id = null) {
		$this->layout='ajax' ;
		$this->Egitim->id = $id;
		if (!$this->Egitim->exists()) {
			//throw new NotFoundException(__('Invalid user'));
		}
		$this->set('egitim', $this->Egitim->read(null, $id));
		/*
		if (!$id) {
			throw new NotFoundException(__('Invalid post'));
		}
		$egitim = $this->Egitim->findById($id);
		if (!$egitim) {
			throw new NotFoundException(__('Invalid post'));
		}
		
		$this->set('egitim', $egitim);
		*/
		
	}
	
	/**
	 * Eğitim dokümanları listelenir
	 */
	public function listEgitimDokuman () {
		$this->layout=null ;
		
		$egitimId = $this->request->query('egitimId');
		debug($this->request->data, true);
		if (!$egitimId) {
			$egitimId = $this->request->data('egitimId');
		}
		
		$egitimDokumani = ClassRegistry::init('EgitimDokumani');
		$data = $egitimDokumani->find('all', array(
			'conditions' => array('EgitimDokumani.egitim_id ' => $egitimId),
		));
		
		$this->set('egitimDokumans', $data);
		$this->set('count', count($data));
		
	}
	
	/**
	 * Eğitim dokümanı kaydedilir
	 */
	public function saveEgitimDokuman () {

		$this->autoRender = false;
		$this->layout = null;
		$media = ClassRegistry::init('Media');
		$egitimDokumani = ClassRegistry::init('EgitimDokumani');
		
		debug($media, true);
		if ($this->request->is('post')) {
			$egitimDokumani->create();
			$result = FALSE;
			$cv_media_id = null;
			$tempFilePath = $_FILES['dokumandata']['tmp_name'];
			$realFileName = $_FILES['dokumandata']['name'];
		
			CakeLog::write('debug', 'filesize: '. filesize($tempFilePath ));
			$fileSize = filesize($tempFilePath);
			$fileData = fread(fopen($tempFilePath, "r"), $fileSize);
		
			$tur = strtolower(end(explode(".", $realFileName)));
			$media->save(array(
					name => $realFileName,
					ext => end(explode(".", $realFileName)),
					content => $fileData,
					size => $fileSize,
					type => $tur
			));
			$cv_media_id = $media->id;
				
			$data = $this->request->data;
			$data['media_id'] = $cv_media_id;
			$data['tur'] = $tur;
			
			$egitimDokumani = ClassRegistry::init('EgitimDokumani');
				
			Debugger::log($data);
			$result = $egitimDokumani->save($data);
			Debugger::log($egitimDokumani);
				
			if ($result) {
				echo "{msg:'Eğitim Dokümanı Başarıyla Kaydedildi', success: true}";
			} else {
				echo "{msg:'Hatalı', success: false}";
			}
		}
				
	}
	
	public function downloadDokuman($mediaId = null) {
		$this->autoRender = false;
		$this->layout = 'ajax';
	
		$this->viewClass = 'Media';
		$media = ClassRegistry::init('Media');
		$media->id = $mediaId;
	
		$mediaData = $media->read()['Media'];
	
		//header('Content-type: ' . 'image/jpeg');
		header('Content-length: ' . $mediaData['size']);
		header('Content-Disposition: attachment; filename='. $mediaData['name']);
	
	
		//debug($media, true);
		echo  $media->read()['Media']['content'];
		exit();
		//echo $cvMediaId;
	}

}
